﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Windows.Forms;// for Panel 
using System.Diagnostics; // for Line number

namespace RobotCleaner
{
    /// <summary>
    /// 传感器类
    /// Sep 23,2013
    /// </summary>
    public class Transducer
    {
        /// <summary>
        /// 传感器颜色
        /// </summary>
        private Color _t_color = Color.Yellow;
        /// <summary>
        /// X 坐标
        /// </summary>
        private int _T_X;
        /// <summary>
        /// Y 坐标
        /// </summary>
        private int _T_Y;
        /// <summary>
        /// 传感器宽
        /// </summary>
        private int _T_Width = 2;
        /// <summary>
        /// 传感器高
        /// </summary>
        private int _T_Height = 2;
        /// <summary>
        /// 传感器坐标
        /// </summary>
        private Point _transducer;
        /// <summary>
        /// 夹角
        /// </summary>
        private int _angle;
        /// <summary>
        /// 传感器构造函数
        /// </summary>
        /// <param name="_p"></param>
        /// <param name="distance"></param>
        /// <param name="angle"></param>
        public Transducer(Point _p, int distance, int degrees)
        {
            double angle = Math.PI * (degrees -90) / 180.0;
            _transducer = _p;
            _transducer.X = _p.X;
            _transducer.Y = _p.Y;
            _T_X = _p.X - (int)(distance * Math.Cos(angle));
            _T_Y = _p.Y + (int)(distance * Math.Sin(angle));
            _angle = degrees;
        }

        /// <summary>
        /// 传感器 X 坐标
        /// </summary>
        public int T_X
        {
            get { return _T_X; }
            set { _T_X = value; }
        }
        /// <summary>
        /// 传感器 Y 坐标
        /// </summary>
        public int T_Y
        {
            get { return _T_Y; }
            set { _T_Y = value; }
        }
        /// <summary>
        /// 夹角
        /// </summary>
        public int Angle
        {
            get { return _angle; }
            set { _angle = value; }
        }
        /// <summary>
        /// 设置角度 增量方式
        /// </summary>
        /// <param name="angle"></param>
        public void setAngle(int angle)
        {
            _angle += angle;
        }
        /// <summary>
        /// 画传感器
        /// </summary>
        /// <param name="r"></param>
        /// <param name="p"></param>
        /// <param name="angle"></param>
        public void drawTransducer(Robot r, Panel p, int degrees)
        {
            try
            {
                using (Graphics g = p.CreateGraphics())
                {
                    double _myAngle = Math.PI * (degrees + r.Angle) / 180.0;
                    _T_X = r.get_Center_X_Y().X - (int)(r.TransducerDistance * Math.Cos(_myAngle));
                    _T_Y = r.get_Center_X_Y().Y + (int)(r.TransducerDistance * Math.Sin(_myAngle));
                    g.FillEllipse(new SolidBrush(_t_color),
                        _T_X,_T_Y,
                        _T_Width, _T_Height
                    );
                }
            }
            catch (Exception ex)
            {
                StackTrace st = new StackTrace(new StackFrame(true));
                StackFrame sf = st.GetFrame(0);
                Console.WriteLine(" File: {0}", sf.GetFileName());                                               
                Console.WriteLine(" Method: {0}", sf.GetMethod().Name);                                 
                Console.WriteLine(" Line Number: {0}", sf.GetFileLineNumber());                 
                Console.WriteLine(" Column Number: {0}", sf.GetFileColumnNumber());
                Console.WriteLine(" Message: {0}", ex.ToString());
            }
        }
    }
}
